package cgl.narada.topicgenerator;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

import cgl.narada.protocol.Protocol;
import cgl.narada.util.ByteUtilities;

public class LongTopicRequest {

   private byte[] data;

   private int clientID;
   private int numberOfTopics;

   /**
    * this constructor is used in client side to construct a request message
    * @param clientID
    */
   public LongTopicRequest(int clientID, int numberOfTopics){
      this.clientID = clientID;
      this.numberOfTopics = numberOfTopics;
   }

   /**
    * this constructor is used server side to construct a request message
    * from a byte stream received over the network
    * @param data the byte array received over the network
    */
   public LongTopicRequest(byte[] data) {
      this.data = data;
      clientID = ByteUtilities.getInt(data, 1);
      this.numberOfTopics = ByteUtilities.getInt(data, 5);
   }

   /**
    * @return the clientID of this object
    */
   public int getClientID(){
      return clientID;
   }

   /**
    * @return the clientID of this object
    */
   public int getNumberOfTopics(){
      return this.numberOfTopics;
   }

   /**
    * this method is used to serialize an object to send accross the network
    * @return the serialized form of the object
    */
   public byte[] getBytes(){

      int length = 1 + 4 + 4;
      byte[] result = new byte[ length ];
      result[0] = Protocol.LONG_TOPIC_EVENT;

      // encode clientID
      byte[] intBytes = ByteUtilities.getBytes(clientID);
      result[1] = intBytes[0];
      result[2] = intBytes[1];
      result[3] = intBytes[2];
      result[4] = intBytes[3];

      // encode numberOfTopics
      intBytes = ByteUtilities.getBytes(this.numberOfTopics);
      result[5] = intBytes[0];
      result[6] = intBytes[1];
      result[7] = intBytes[2];
      result[8] = intBytes[3];

      return result;
   }

}